package Client.Dropbox

/**
 * Object to start the service running via Apache Tomcat
 */
object DropboxService {
  println("in service object")
  private var stopped=false
  
  /**
   * Method that receives the start and stop parameters from Tomcat
   * @param args the arguments that tell whether to start or stop
   */
  def windowsService (args:Array[String]){
    println("in service")
    var cmd="start"
    if (args.length>0)
      cmd=args.first
    if ("start".equals(cmd))
      start
    else
      stop
  }
  
  /**
   * Method to start the service
   */
  def start {
    println("in start")
    stopped=false
    var thread=new Thread(new Runnable {
      def run = DropboxMain.main(null)
    })
    thread.start
    while (!stopped){
      this.synchronized {
        try {
          this.wait(60000)
        }
        catch {
          case e:Exception => System.err.println(e.getMessage)
        }
      }
    }
  }
  
  /**
   * Method to stop the service
   */
  def stop {
    println("in stop")
    stopped=true
    DropboxMain.stop
    this.synchronized {
      try {
        this.notify
      }
      catch {
        case (e:Exception) => System.err.println(e.getMessage)
      }
    }
  }

}